package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.adapter.ReportDetailListAdapter;
import com.hxkcloud.nurse.beans.MyReportValueBean;
import com.hxkcloud.nurse.beans.ReportValueBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.activitys
 * Description:全部记录
 * Create by Ray.
 * Date: 2016/10/9 18:06
 */
public class MyAllReportListActivity extends BaseActivity implements View.OnClickListener, ReportDetailListAdapter.PostOptionsListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private RecyclerView recyclerView;
    private ReportDetailListAdapter adapter;

    private List<ReportValueBean> reportValueBeans;
    private ObjectResultBean<MyReportValueBean> resultBean;

    private String startDateStr = "";
    private String endDateStr = "";

    private String reportId = "";//报告Id
    private String reportName = "";//报告名

    private String unit = "";

    float max = 0;
    float min = 0;

    private String patientId;
    private TextView dataUnitTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_myallreportlist);
        reportValueBeans = new ArrayList<>();
        if (getIntent().hasExtra("reportId")) {
            reportId = getIntent().getStringExtra("reportId");
        }
        if (getIntent().hasExtra("reportName")) {
            reportName = getIntent().getStringExtra("reportName");
        }
        if (getIntent().hasExtra("startDate")) {
            startDateStr = getIntent().getStringExtra("startDate");
        }
        if (getIntent().hasExtra("endDate")) {
            endDateStr = getIntent().getStringExtra("endDate");
        }
        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        if (getIntent().hasExtra("unit")) {
            unit = getIntent().getStringExtra("unit");
        }
        if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient)) {
            patientId = loginInfo.getPatientId();
        }
        initView();
//        initData();
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DELETEMYREPORT_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    loading();
                    YiJiaHuNetWorkUtils.getMyreportDetail(patientId, reportId, "", "", handler);
                }
                break;
            case MessageCode.NETWORKCODE_DELETEMYREPORT_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;
            case MessageCode.NETWORKCODE_MYREPORTDETAIL_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyReportValueBean>) msg.obj;
                    reportValueBeans.clear();
                    reportValueBeans = resultBean.getData().getList();
                    if (!"".equals(resultBean.getData().getMaxValue())) {
                        max = Float.parseFloat(resultBean.getData().getMaxValue());
                    }

                    if (!"".equals(resultBean.getData().getMaxValue())) {
                        min = Float.parseFloat(resultBean.getData().getMinValue());
                    }
                    if (reportValueBeans != null && reportValueBeans.size() > 0) {
                        adapter.setReferMaxValue(resultBean.getData().getUpperValue());
                        adapter.setReferMinValue(resultBean.getData().getLowerValue());
                        adapter.setData(reportValueBeans);
                    }else{
                        adapter.setReferMaxValue(resultBean.getData().getUpperValue());
                        adapter.setReferMinValue(resultBean.getData().getLowerValue());
                        adapter.setData(reportValueBeans);
                    }
                }

                break;
            case MessageCode.NETWORKCODE_MYREPORTDETAIL_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText("全部记录");

        dataUnitTV = (TextView) findViewById(R.id.allreportlist_tv_dataunit);
        dataUnitTV.setText(getResources().getString(R.string.reportinput_text_data) + unit);
        dataUnitTV.setVisibility(View.VISIBLE);

        recyclerView = (RecyclerView) findViewById(R.id.allreportlist_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);

        adapter = new ReportDetailListAdapter(this, reportValueBeans, recyclerView, loginInfo.getUserStyle(), this);
        recyclerView.setAdapter(adapter);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getMyreportDetail(patientId, reportId, "", "", handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }

    }

    @Override
    public void PostOptions(View view, int position, String optionType) {
        if ("delete".equals(optionType)) {//删除操作
            loading();
            YiJiaHuNetWorkUtils.deleteMyReport(reportValueBeans.get(position).getValueId(), handler);
        } else if ("edit".equals(optionType)) {//编辑界面
            Intent intent = new Intent(mContext, MyReportInputActivity.class);
            intent.putExtra("reportId", reportId);
            intent.putExtra("reportName", reportName);
            intent.putExtra("time", reportValueBeans.get(position).getTime());
            intent.putExtra("data", reportValueBeans.get(position).getValue());
            intent.putExtra("valueid", reportValueBeans.get(position).getValueId());
            intent.putExtra("picPath", reportValueBeans.get(position).getPic());
            intent.putExtra("max", max);
            intent.putExtra("min", min);
            mContext.startActivity(intent);
        } else if ("displayImg".equals(optionType)) {
            Intent iPhotos = new Intent(mContext, DisplayImageActivity.class);
            iPhotos.putExtra("path", reportValueBeans.get(position).getPic());
            iPhotos.putExtra("isHideDelete", "true");
            iPhotos.putExtra("position", 0);
            List<String>  picList=new ArrayList<>();
            //for (int i = 0; i < reportValueBeans.size(); i++) {
                picList.add(reportValueBeans.get(position).getPic());
            //}
            iPhotos.putStringArrayListExtra("pics", (ArrayList<String>) picList);
            startActivity(iPhotos);
        }
    }
}
